--
-- Author: yawei.zhu
-- Date: 2018-10-10 10:31:40
--
-- BossGuildRankRender
--
local NewguildIconRender = require("app.newGuild.views.renders.NewguildIconRender")

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.BossGuildRankRender))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self._ui.Button_1:setVisible(false)
    self._ui.Button_2:setVisible(true)

    self._ui.Button_2:addClickEventListener(function()
        if self._data then
            local guild = self._data[2]
            if guild then
                local guildModel = app:getInst("NewguildModel")
                guildModel:reqApplyGuild( guild.guildID )
            end
        end
    end)

    for i = 1, 3 do
        self["rank"..i] = self._ui["Panel_"..i]
    end
    self._ui.num:setVisible(true)
    self.myBG = self._ui["Panel/Panel_bg/bg_my"]
    self:setContentSize(self._ui.Panel:getContentSize())

    self:refresh()
end

function ClassRef:refresh()
    if not self._data then return end
    self:clearItem()

    local rank = self._data[1]
    local guild = self._data[2]  

    if guild.guildID == UD:getGuildId() then
        self.myBG:setVisible(true)
        self._ui.Button_2:setVisible(false)
    end

    local iconRender = NewguildIconRender.create(self._ui.icon) -- 公会icon
    iconRender:setData( {baseIconId = guild.FlagBaseIcon, additionIconId = guild.FlagAdditionalIcon, baseIconColor = guild.FlagBaseIconColor, additionIconColor = guild.FlagAdditionalIconColor} ) 

    if rank <= 3 then
        self["rank"..rank]:setVisible(true)
    else
        self._ui.num:setString(rank)
    end

    self._ui.name:setString(guild.guildName)
    self._ui.job:setString(guild.guildMemberCount)
    self._ui.time:setString(guild.killedCount)
end

function ClassRef:clearItem()
    self.myBG:setVisible(false)
    self._ui.num:setString("")
    for i = 1, 3 do
        self["rank"..i]:setVisible(false)
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
